/*
 * Polynomial.cpp
 *
 *  Created on: 17/06/2011
 *      Author: Idan
 */

#include "Polynomial.h"
Polynomial::Polynomial(const Matrix& A, const Matrix& B,
		int priority, const int jobID):Job(A,B,priority,jobID) {}
Matrix Polynomial::execute(){
	if(matrix1.rows()!=matrix1.columns() || matrix2.columns()!=1){
		throw OperationFailed();
	}
	int matrixDimensions = matrix1.rows();
	Matrix matrix1Power = Matrix::createUnit(matrixDimensions);
	Matrix result(matrixDimensions,matrixDimensions);
	for (int i = 1; i <= matrix2.rows(); ++i) {
		for (int j = 1; j < i; ++j) {
			matrix1Power *= matrix1;
		}
		result += matrix1Power*matrix2(i,1);
		matrix1Power = Matrix::createUnit(matrixDimensions);
	}
	return result;
}

